//
//  JMViewController.m
//  YaoYao
//
//  Created by JM Zhao on 16/9/18.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "JMViewController.h"
#import "JMAlbumController.h"
#import "JMTrashController.h"
#import "JMFunsController.h"
#import "JMMainNavController.h"
#import "JMTabBar.h"

@interface JMViewController ()

@property (nonatomic, strong) JMAlbumController *homeVC;
@property (nonatomic, strong) JMTrashController *findVC;
@property (nonatomic, strong) JMFunsController *meVC;
@property (nonatomic, assign) BOOL isBool;
@end

@implementation JMViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupChildViewController];
    
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 49)];
    backView.backgroundColor = [UIColor blueColor];
    [self.tabBar insertSubview:backView atIndex:0];
    self.tabBar.opaque = YES;
    
    JMTabBar *tabBar = [[JMTabBar alloc] initWithFrame:self.tabBar.frame];
    [self setValue:tabBar forKeyPath:@"tabBar"];
}

- (void)setupChildViewController
{
    // 聊天
    JMAlbumController *homeVC = [[JMAlbumController alloc] init];
    _homeVC = homeVC;
    [self setupChildViewController:homeVC
                             image:[[UIImage imageWithRenderingName:@"photos_unselect"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]
                          selImage:[[UIImage imageNamed:@"photos"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]
                             title:@"Album"
                               tag:baseTag+0];
    
    // 发现
    JMTrashController *findVC = [[JMTrashController alloc] init];
    _findVC = findVC;
    [self setupChildViewController:findVC
                             image:[[UIImage imageWithRenderingName:@"photos_unselect"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]
                          selImage:[[UIImage imageNamed:@"photos"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]
                             title:@"Trash"
                               tag:baseTag+2];
    
    // 设置
    JMFunsController *meVC = [[JMFunsController alloc] init];
    _meVC = meVC;
    [self setupChildViewController:meVC
                             image:[[UIImage imageWithRenderingName:@"fun_unselect"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]
                          selImage:[[UIImage imageNamed:@"fun"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]
                             title:@"Funs"
                               tag:baseTag+3];    
}

#pragma mark - 设置tabBar抽取方法
- (void)setupChildViewController:(UIViewController *)vc image:(UIImage *)image selImage:(UIImage *)selImage title:(NSString *)title tag:(NSInteger)tag
{
    vc.tabBarItem.tag = tag;
    vc.title = title;
    
    NSMutableDictionary *attrNol = [NSMutableDictionary dictionary];
    attrNol[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
    [vc.tabBarItem setTitleTextAttributes:attrNol forState:UIControlStateNormal];
    NSMutableDictionary *attrSel = [NSMutableDictionary dictionary];
    attrSel[NSForegroundColorAttributeName] = @"PYTHEME_THEME_COLOR";
    
    // 设置图片
    [vc.tabBarItem setImage:image];
    [vc.tabBarItem setSelectedImage:selImage];
    
    // 导航视图控制器
    JMMainNavController *nav = [[JMMainNavController alloc] initWithRootViewController:vc];
    [self addChildViewController:nav];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
